/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladores;

import io.Alumno;
import io.Juicio;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author NiCo
 */
public class ControladorPrincipal {

    private ControladorPrincipal() {
    }

    public static ControladorPrincipal getInstance() {
        return PrincipalHolder.INSTANCE;
    }

    private static class PrincipalHolder {

        private static final ControladorPrincipal INSTANCE = new ControladorPrincipal();
        private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("BoletinCalificacionesPU");
        private static final EntityManager em = emf.createEntityManager();
        private static final Properties propiedades = new Properties();
    }

    public EntityManager getEntity() {
        return PrincipalHolder.em;
    }

    public void guardar(Object object) {
        getEntity().getTransaction().begin();
        try {
            getEntity().persist(object);
            getEntity().getTransaction().commit();
        } catch (Exception e) {
            getEntity().getTransaction().rollback();
        }
    }

    public void eliminar(Object object) {
        getEntity().getTransaction().begin();
        try {
            getEntity().remove(object);
            getEntity().getTransaction().commit();
        } catch (Exception e) {
            getEntity().getTransaction().rollback();
        }
    }

    public void cerrarEntity() {
        PrincipalHolder.em.close();
    }

    public List<Integer> grupos(int anio, String curso) {
        List<Integer> lista = null;
        ControladorPrincipal.getInstance().getEntity().getTransaction().begin();
        try {
            lista = ControladorPrincipal.getInstance().getEntity().createNativeQuery("SELECT DISTINCT nroGrupo FROM alumno WHERE curso = '" + curso + "' AND anio = " + anio + ";").getResultList();
            ControladorPrincipal.getInstance().getEntity().getTransaction().commit();
        } catch (Exception e) {
            ControladorPrincipal.getInstance().getEntity().getTransaction().rollback();
        }
        return lista;
    }

    public List<Alumno> alumnosDelGrupo(String curso, int grupo, int anio) {
        List<Alumno> lista = null;
        ControladorPrincipal.getInstance().getEntity().getTransaction().begin();
        try {
            lista = ControladorPrincipal.getInstance().getEntity().createNativeQuery("SELECT * FROM alumno WHERE curso = '" + curso + "' AND nroGrupo = " + grupo + " AND anio = " + anio + ";", Alumno.class).getResultList();
            ControladorPrincipal.getInstance().getEntity().getTransaction().commit();
        } catch (Exception e) {
            ControladorPrincipal.getInstance().getEntity().getTransaction().rollback();
        }
        return lista;
    }

    public Juicio juicioAlumno(Alumno al, String materia, int reunion, int anio) {
        Juicio juicio = null;
        ControladorPrincipal.getInstance().getEntity().getTransaction().begin();
        try {
            juicio = (Juicio) ControladorPrincipal.getInstance().getEntity().createNativeQuery("SELECT * FROM juicio WHERE alumno_id = " + al.getId() + " AND materia = '" + materia + "' AND YEAR(fecha) = " + anio + " AND reunion = " + reunion + ";", Juicio.class).getSingleResult();
            ControladorPrincipal.getInstance().getEntity().getTransaction().commit();
        } catch (Exception e) {
            ControladorPrincipal.getInstance().getEntity().getTransaction().rollback();
        }
        return juicio;
    }
}
